<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div>
      <div id="btns">
        <button data-path="/news">新闻</button>
        <button data-path="/music">音乐</button>
        <button data-path="/entertainment">娱乐</button>
        <hr />

        <div id="view"></div>
      </div>
    </div>

    <script>
      let oBtns = document.querySelectorAll("#btns button");
      let oViews = document.querySelector("#view");

      let routes = [
        {
          path: "/news",
          content: "新闻模块",
        },
        {
          path: "/music",
          content: "音乐模块",
        },
        {
          path: "/entertainment",
          content: "娱乐模块",
        },
      ];

      for (var i = 0; i < oBtns.length; i++) {
        oBtns[i].onclick = function () {
          history.pushState(null, null, this.dataset.path);

          routes.forEach((item) => {
            if (item.path === this.dataset.path) {
              oViews.textContent = item.content;
            }
          });
        };
      }

      window.onpopstate = function () {
        console.log(location.pathname);

        routes.forEach((item) => {
          if (item.path === location.pathname) {
            oViews.textContent = item.content;
          }
        });
      };
    </script>
  </body>
</html>
